/*

  The contents of this file are subject to the Mozilla Public License Version
  1.1 (the "License"); you may not use this file except in compliance with
  the License. You may obtain a copy of the License at 
  
           http://www.mozilla.org/MPL/ 
  
  Software distributed under the License is distributed on an "AS IS" basis,
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  for the specific language governing rights and limitations under the License. 
  
  The Original Code is Indigo framework applicatif.
  
  The Initial Developer of the Original Code is
  Olivier Bugalotto (aka Iteratif) <olivier.bugalotto@iteratif.net>.
  Portions created by the Initial Developer are Copyright (C) 2004-2008
  the Initial Developer. All Rights Reserved.
  
  Contributor(s) :
  
*/
package indigo.collections
{
	import flash.utils.Dictionary;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;
	/**
	 * Implementation de l'interface <code>IDictionary</code>.<br>
	 * La classe <code>Hashtable</code> represente une collection non generique d'objets
	 * <br>de paires cle/valeur.
	 * 
	 */
	public class Hashtable extends Proxy implements IDictionary
	{
		private var _items:Dictionary;
		private var _count:uint = 0;
		
		/**
		 * 
		 * @return 
		 * @inheritDoc
		 */
		public function get count():uint
		{
			return _count;
		}
		
		/**
		 * 
		 * @return 
		 * @inheritDoc
		 */
		public function get keys():ICollection
		{
			var ks:ArrayList = new ArrayList();
			for(var key:Object in _items)
				ks.add(key);
			return ks;
		}
		
		/**
		 * 
		 * @return 
		 * @inheritDoc
		 */
		public function arrayTo(index:int = 0):Array
		{
			var array:Array = [];
			for(var key:Object in _items)
				array.push( {key:key, value:_items[key] } );
			return array;
		}
		
		/**
		 * 
		 * @return 
		 * @inheritDoc
		 */
		public function get values():ICollection
		{
			var values:ArrayList = new ArrayList();
			for each(var value:Object in _items)
				values.add(value);
			return values;
		}
		
		/**
		 * Creer un nouvel objet <code>Hashtable</code> 
		 * 
		 */
		public function Hashtable()
		{
			_items = new Dictionary(true);
		}

		/**
		 * 
		 * @return 
		 * @inheritDoc
		 */
		public function getEnumerator():IEnumerator
		{
			return new Enumerator(arrayTo());
		}
		
		/**
		 * 
		 * @param key
		 * @param item
		 * @inheritDoc
		 */
		public function addItem(key:Object, item:Object):void
		{
			if(!key) throw new ArgumentError();
			
			if(!contains(key)) {
				_items[key] = item;
				_count++;
			}
		}
		
		/**
		 * 
		 * @param key
		 * @return 
		 * @inheritDoc
		 */
		public function getValue(key:Object):Object {
			if(!key) throw new ArgumentError("La clé ne peut pas être de référence null");
			return _items[key];
		}
		
		/**
		 * 
		 * @inheritDoc
		 */
		public function clear():void
		{
			for(var key:Object in _items)
				removeItem(key);
		}
		
		/**
		 * 
		 * @param key
		 * @return 
		 * @inheritDoc
		 */
		public function contains(key:Object):Boolean
		{
			var value:Object = _items[key];
			return Boolean(value);
		}
		
		/**
		 * 
		 * @param key
		 * @inheritDoc
		 */
		public function removeItem(key:Object):void
		{
			if(contains(key)) {
				_items[key] = null;
				_count--;
			}
		}
		
		/**
		 * Definit un element depuis un index specifie de la collection
		 * <br>a l'aide de la syntaxe [].
		 * @param name
		 * @param value
		 * 
		 */
		flash_proxy override function setProperty(key:*, value:*):void {
			addItem(key,value);
		}
		
		/**
		 * Obtenir un element depuis son index<br>
		 * a l'aide de la syntaxe [].
		 * 
		 * @param name
		 * @return 
		 * 
		 */
		flash_proxy override function getProperty(key:*):* {
			return getValue(key);
		}
		
		public function toString():String {
			return keys.arrayTo().toString();
		}
	}
}